1
|
|
|
import React from "react" |
2
|
|
|
import expectRender from "../expect-to-same-render" |
3
|
|
|
import type { ClassNames, ClassValue } from "./defs" |
4
|
|
|
import classNamingBasic from "./basic" |
5
|
|
|
import classNamesCheck from "./check" |
6
|
|
|
|
7
|
|
|
function Button({className, "classnames": { Btn }}: ClassNames<true, "Btn">) { |
8
|
|
|
return <button {...classNamingBasic(className, { Btn })}/> |
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
function Root({ |
12
|
|
|
classnames, "classnames": { App__Item, App__Footer } |
13
|
|
|
}: ClassNames<"App__Item"|"App__Footer", typeof Button>) { |
14
|
|
|
return <> |
15
|
|
|
<Button {...{ |
16
|
|
|
...classNamingBasic({ App__Item }), |
17
|
|
|
classnames |
18
|
|
|
}}/> |
19
|
|
|
<div |
20
|
|
|
className={classNamingBasic<string>({App__Footer})} |
21
|
|
|
data-classname={`${classNamingBasic({App__Footer})}`} |
22
|
|
|
/> |
23
|
|
|
</> |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
it("not css module", () => expectRender( |
27
|
|
|
<Root classnames={classNamesCheck()}/> |
28
|
|
|
).toSame( |
29
|
|
|
<button className="App__Item Btn"></button>, |
30
|
|
|
<div className="App__Footer" data-classname="App__Footer"></div> |
31
|
|
|
)) |
32
|
|
|
|
33
|
|
|
it("css module", () => expectRender( |
34
|
|
|
<Root classnames={{ |
35
|
|
|
App__Footer: "footer-hash", |
36
|
|
|
App__Item: "item-hash", |
37
|
|
|
Btn: "btn-hash" |
38
|
|
|
}}/> |
39
|
|
|
).toSame( |
40
|
|
|
<button className="item-hash btn-hash"></button>, |
41
|
|
|
<div className="footer-hash" data-classname="footer-hash"></div> |
42
|
|
|
)) |
43
|
|
|
|
44
|
|
|
it("vscode couldn't rename enum element", () => { |
45
|
|
|
function Root({ |
46
|
|
|
"classnames": {App: App__Container} |
47
|
|
|
}: ClassNames<"App">) { |
48
|
|
|
return <div {...classNamingBasic({App: App__Container})}/> |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
expectRender( |
52
|
|
|
<Root classnames={classNamesCheck()} /> |
53
|
|
|
).toSame( |
54
|
|
|
<div className="App"></div> |
55
|
|
|
) |
56
|
|
|
}) |
57
|
|
|
|
58
|
|
|
it("vscode can rename property", () => { |
59
|
|
|
type RootProps = { |
60
|
|
|
classnames: { |
61
|
|
|
App__Container: ClassValue |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
function Root({ |
66
|
|
|
"classnames": {App__Container: App__Container} |
67
|
|
|
}: RootProps) { |
68
|
|
|
return <div {...classNamingBasic({App: App__Container})}/> |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
function Root2({ |
72
|
|
|
"classnames": { |
73
|
|
|
// VSCode didn't rename here due to `ClassNames<>` wrapper |
74
|
|
|
//@ts-expect-error Property 'App' does not exist |
75
|
|
|
App: App__Container |
76
|
|
|
} |
77
|
|
|
}: ClassNames<typeof Root>) { |
78
|
|
|
return <div {...classNamingBasic({App: App__Container})}/> |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
expectRender( |
82
|
|
|
<Root |
83
|
|
|
//TODO Solve it |
84
|
|
|
//@ts-expect-error Property 'App' is missing in type 'Record<string, |
85
|
|
|
classnames={ |
86
|
|
|
classNamesCheck()} />, |
87
|
|
|
<Root2 classnames={classNamesCheck()} /> |
88
|
|
|
).toSame( |
89
|
|
|
<div className="App"/>, |
90
|
|
|
<div className="App"/> |
91
|
|
|
) |
92
|
|
|
}) |
93
|
|
|
|
94
|
|
|
it("additional type check after rename", () => { |
95
|
|
|
type Props1 = { |
96
|
|
|
classnames: { class1: ClassValue } |
97
|
|
|
} |
98
|
|
|
type Props2 = { |
99
|
|
|
classnames: { class2_renamed: ClassValue } |
100
|
|
|
} |
101
|
|
|
const { class1, |
102
|
|
|
//@ts-expect-error Property 'class2' does not exist |
103
|
|
|
class2 |
104
|
|
|
} = classNamesCheck<Props1 & Props2>(); |
105
|
|
|
|
106
|
|
|
expectRender( |
107
|
|
|
<div {...classNamingBasic<Props1>({class1})} />, |
108
|
|
|
<div {...classNamingBasic<Props2>({ |
109
|
|
|
//@ts-expect-error Object literal may only specify known properties, and 'class2' does not exist |
110
|
|
|
class2 |
111
|
|
|
})} /> |
112
|
|
|
).toSame( |
113
|
|
|
<div className="class1"/>, |
114
|
|
|
<div className="class2"/> |
115
|
|
|
) |
116
|
|
|
}) |
117
|
|
|
|